<?php

Raise::load('core.RaiseCollection');
Raise::load('core.RaiseVariable');
Raise::load('core.RaiseKeyValuePair');

/**
 * RaiseHttpHeaderCollection class
 * A collection of HTTP headers
 *
 * @author Sam-Mauris Yong <hellclanner at live dot com>
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.Core.DataStructure.Collection
 * @since 1.2
 */
class RaiseHttpHeaderCollection extends RaiseCollection {

    /**
     * Add a new header to the collection.
     * If the replace parameter is set to true, the header currently in the collection that has the same key will be replaced.
     * Else the value will be set to a sub-collection
     * @param string|RaiseKeyValuePair $keyOrPair The key of the header or the RaiseKeyValuePair to enter.
     * @param mixed|boolean $valueOrReplace (optional) If RaiseKeyValuePair is entered for the 1st parameter, this parameter must be a boolean. If not set, this will be assumed as TRUE. Or it will contain the value of header to add if first parameter is a string.
     * @param boolean $replace (optional) If adding using key / value, the 3rd parameter must be a boolean indicating whether or not to replace the header.
     */
    public function add(){
        $a = func_get_args();
        if(count($a) == 3)
        {
            if(!RaiseVariable::isType($a[0], RaiseVariable::TYPESTRING)){
                throw new RaiseInvalidArgumentException('The argument $keyOrValue must be string when the second argument $value exists');
            }
            if(array_key_exists($a[0], $this->_a) && !$a[2]){
                if(!is_array($this->_a[$a[0]])){
                    $t = $this->_a[$a[0]];
                    $this->_a[$a[0]] = array($t);
                }
                $this->_a[$a[0]][] = $a[1];
            }else{
                $this->_a[$a[0]] = $a[1];
            }
        }
        elseif(count($a) == 2 && $a[0] instanceof RaiseKeyValuePair)
        {
            if(count($a) == 1){
                $a[1] = true;
            }
            if(array_key_exists($a[0]->key(), $this->_a) && !$a[1]){
                if(!is_array($this->_a[$a[0]->key()])){
                    $t = $this->_a[$a[0]->key()];
                    $this->_a[$a[0]->key()] = array($t);
                }
                $this->_a[$a[0]->key()][] = $a[0]->value();
            }else{
                $this->_a[$a[0]->key()] = $a[0]->value();
            }
        }
        elseif(count($a) == 2)
        {
            if(!RaiseVariable::isType($a[0], RaiseVariable::TYPESTRING)){
                throw new RaiseInvalidArgumentException('The argument $keyOrValue must be string when the second argument $value exists');
            }
            $replace = true;
            if(array_key_exists($a[0], $this->_a) && !$replace){
                if(!is_array($this->_a[$a[0]])){
                    $t = $this->_a[$a[0]];
                    $this->_a[$a[0]] = array($t);
                }
                $this->_a[$a[0]][] = $a[1];
            }else{
                $this->_a[$a[0]] = $a[1];
            }
        }
        elseif(count($a) == 1)
        {
            if(!RaiseVariable::isType($a[0], RaiseVariable::TYPESTRING)){
                throw new RaiseInvalidArgumentException('The argument $keyOrValue must be string when the second argument $value exists');
            }
            $replace = true;
            if(array_key_exists($a[0], $this->_a) && !$replace){
                if(!is_array($this->_a[$a[0]])){
                    $t = $this->_a[$a[0]];
                    $this->_a[$a[0]] = array($t);
                }
                $this->_a[$a[0]][] = '';
            }else{
                $this->_a[$a[0]] = '';
            }
        }
        else
        {
            throw new RaiseInvalidArgumentException('The argument count is invalid. Should be add($key, $value) or add($value)');
        }
        $this->count = false;
    }
    

}

